<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="半成品" prop="sfproductName">
        <el-input
          v-model="queryParams.sfproductName"
          placeholder="请输入半成品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="排产时间" prop="schedulingTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.schedulingTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择排产时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="车间产出数量" prop="workshopOutputNumber" label-width="100px"  style="white-space: nowrap;">-->
<!--        <el-input-->
<!--          v-model="queryParams.workshopOutputNumber"-->
<!--          placeholder="请输入车间产出数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:course:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:course:export']"
        >导出</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection"  align="center" />
      <el-table-column label="id" align="center" prop="id" sortable/>
      <el-table-column label="半成品" align="center" prop="sfproductName" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="参数" align="center" prop="parameter" />
      <el-table-column label="排产时间" align="center" prop="schedulingTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.schedulingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车间产出数量" align="center" prop="workshopOutputNumber" sortable/>
      <el-table-column label="后焊" align="center" prop="solderId">
        <template slot-scope="scope">
          <a @click="showSolder(scope.row)" >{{ scope.row.solderId }}</a>
        </template>
      </el-table-column>

      <el-table-column label="半成品入库" align="center" prop="acceptanceNumber">
        <template slot-scope="scope">
          <a @click="showWarehousinginfo1(scope.row)">{{scope.row.acceptanceNumber}}</a>
        </template>
      </el-table-column>
      <el-table-column label="半成品检测" align="center" prop="firstDetectionId" >
        <template slot-scope="scope">
          <a @click="showDetection1(scope.row)">{{ scope.row.firstDetectionId }} </a>
          <!--          <i class="el-icon-success" v-if="isInStock(scope.row)" @click="addWarehouse(scope.row)"></i>-->
        </template>
      </el-table-column>
      <el-table-column label="装配/老化" align="center" prop="assembleAgingId" >
        <template slot-scope="scope">
          <a @click="showAssembleAging(scope.row)">{{ scope.row.assembleAgingId }}</a>
        </template>
      </el-table-column>
      <el-table-column label="成品检测" align="center" prop="twiceDetectionId">
        <template slot-scope="scope">
          <a @click="showDetection2(scope.row)">{{ scope.row.twiceDetectionId }} </a>
        </template>
      </el-table-column>
      <el-table-column label="成品入库" align="center" prop="disqualificationNumber">
        <template slot-scope="scope">
          <a @click="showWarehousinginfo2(scope.row)">{{scope.row.disqualificationNumber}}</a>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-cpu"
            @click="weldAdd(scope.row)"
            v-hasPermi="['course:course:weldAdd']"
          >操作</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coordinate"
            @click="detectionAdd(scope.row)"
            v-hasPermi="['course:course:detectionAdd']"
          >检测</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['course:course:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="addWarehouse(scope.row)"
            v-hasPermi="['course:course:addWarehouse']"
          >半成品入库</el-button>
          <el-button
            v-if="scope.row.workshopOutputNumber === 0"
            size="mini"
            type="text"
            icon="el-icon-remove"
            @click="outWarehouse(scope.row)"
            v-hasPermi="['course:course:outWarehouse']"
          >半成品出库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="addFpWarehouse(scope.row)"
            v-hasPermi="['course:course:addFpWarehouse']"
          >成品入库</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['course:course:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 查看操作弹出框 -->
    <el-dialog :visible.sync="dialogVisible" :title="title">
      <el-table :data="dialogTableData" style="width: 100%">
        <el-table-column prop="time" label="操作时间"></el-table-column>
        <el-table-column prop="userName" label="员工"></el-table-column>
        <el-table-column prop="completed" label="操作数"></el-table-column>
        <el-table-column prop="takeoutMaterials" label="备注"></el-table-column>
      </el-table>
    </el-dialog>

    <!-- 查看入库记录弹出框 -->
    <el-dialog :visible.sync="isInfoVisible" :title="title">
      <el-table :data="infoTableData" style="width: 100%">
        <el-table-column prop="time" label="操作时间"></el-table-column>
        <el-table-column prop="userName" label="员工"></el-table-column>
        <el-table-column prop="completed" label="操作数"></el-table-column>
        <el-table-column prop="takeoutMaterials" label="备注"></el-table-column>
      </el-table>
    </el-dialog>

    <!-- 查看检测弹出框 -->
    <el-dialog :visible.sync="isVisible" :title="title">
      <el-table :data="Dialogue" style="width: 100%">
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="time" label="操作时间"></el-table-column>
        <el-table-column prop="userName" label="员工"></el-table-column>
        <el-table-column prop="acceptanceNumber" label="合格数"></el-table-column>
        <el-table-column prop="disqualificationNumber" label="不合格数"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" @click="deleteRow(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产过程管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="半成品" prop="sfproductId" >
          <el-select v-model="form.sfproductId" filterable placeholder="请选择" :disabled = "first">
            <el-option
              v-for="(item, index) in sfproducts"
              :key="item.sfproductId "
              :label="item.model + ' - ' + sfproductNames[index]"
              :value="item.sfproductId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排产时间" prop="schedulingTime" >
          <el-date-picker clearable
            v-model="form.schedulingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择排产时间"
            :disabled = "first">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车间产出数量" prop="workshopOutputNumber" >
          <el-input v-model="form.workshopOutputNumber" placeholder="请输入车间产出数量" :disabled = "first"/>
        </el-form-item>
<!--        <el-form-item label="后焊" prop="solderId" v-show="first">-->
<!--          <el-input v-model="form.solderId" placeholder="请输入后焊" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="首次检测" prop="firstDetectionId" v-show="first">-->
<!--          <el-input v-model="form.firstDetectionId" placeholder="请输入首次检测" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="装配/老化" prop="assembleAgingId" v-show="first">-->
<!--          <el-input v-model="form.assembleAgingId" placeholder="请输入装配/老化" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="二次检测" prop="twiceDetectionId" v-show="first">-->
<!--          <el-input v-model="form.twiceDetectionId" placeholder="请输入二次检测" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="合格数" prop="acceptanceNumber" v-show="first">-->
<!--          <el-input v-model="form.acceptanceNumber" placeholder="请输入合格数" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="不合格数" prop="disqualificationNumber" v-show="first">-->
<!--          <el-input v-model="form.disqualificationNumber" placeholder="请输入不合格数" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改生产流程操作对话框 -->
    <el-dialog :title="title" :visible.sync="openWeld" width="500px" append-to-body>
      <el-form ref="formWeld" :model="formWeld" :rules="rules" label-width="80px">
        <el-form-item label="生产记录id" prop="courseId" v-if="showFormItem">
          <el-input v-model="formWeld.courseId" placeholder="请输入生产记录id" />
        </el-form-item>
<!--        <el-form-item label="操作时间" prop="time">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="formWeld.time"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="请选择操作时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="操作员工id" prop="employeeId">-->
<!--          <el-input v-model="formWeld.employeeId" placeholder="请输入操作员工id" />-->
<!--        </el-form-item>-->
        <el-form-item label="操作流程" prop="flow">
          <el-radio-group v-model="formWeld.flow">
            <el-radio
              v-for="dict in dict.type.lw_flow_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="已完成" prop="completed">
          <el-input v-model="formWeld.completed" placeholder="请输入目前已完成数量" />
        </el-form-item>
        <el-form-item label="备注" prop="takeoutMaterials">
          <el-input v-model="formWeld.takeoutMaterials" placeholder="备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormWeld">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改检测信息对话框 -->
    <el-dialog :title="title" :visible.sync="openDetection" width="800px" append-to-body>
      <el-form ref="formDetection" :model="formDetection" :rules="rules" label-width="80px">
        <el-form-item label="生产记录id" prop="courseId" v-if="showFormItem">
          <el-input v-model="formDetection.courseId" placeholder="请输入生产记录id" />
        </el-form-item>
        <el-form-item label="操作时间" prop="time" v-if="showFormItem">
          <el-date-picker clearable
                          v-model="formDetection.time"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作用户id" prop="userId" v-if="showFormItem">
          <el-input v-model="formDetection.userId" placeholder="请输入操作用户id" />
        </el-form-item>
        <el-form-item label="合格数" prop="acceptanceNumber">
          <el-input v-model="formDetection.acceptanceNumber" placeholder="请输入合格数" />
        </el-form-item>
        <el-form-item label="不合格数" prop="disqualificationNumber">
          <el-input v-model="formDetection.disqualificationNumber" placeholder="请输入不合格数" />
        </el-form-item>
        <el-form-item label="检测类型" prop="status">
          <el-radio-group v-model="formDetection.status">
            <el-radio
              v-for="dict in dict.type.lw_status_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formDetection.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormDetection">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 仓库入库对话框 -->
    <el-dialog :title="title" :visible.sync="openWarehouse" width="800px" append-to-body>
      <el-form ref="formWarehouse" :model="formWarehouse" :rules="rules" label-width="80px">
        <el-form-item label="半成品id" prop="sfproductId">
          <el-input v-model="formWarehouse.sfproductId" placeholder="请输入半成品id" :disabled="true" />
        </el-form-item>
        <el-form-item label="半成品名称" prop="sfproductName" v-show="true">
          <el-input v-model="formWarehouse.sfproductName" placeholder="请输入半成品名称" :disabled="true" />
        </el-form-item>
<!--        <el-form-item label="型号" prop="model">-->
<!--          <el-input v-model="formOutWarehouse.model" placeholder="请输入数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="formWarehouse.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formWarehouse.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormWarehouse">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 仓库入库对话框 -->
    <el-dialog :title="title" :visible.sync="openOutWarehouse" width="800px" append-to-body>
      <el-form ref="formOutWarehouse" :model="formOutWarehouse" :rules="rules" label-width="80px">
        <el-form-item label="半成品id" prop="sfproductId">
          <el-input v-model="formOutWarehouse.sfproductId" placeholder="请输入半成品id" :disabled="true" />
        </el-form-item>
        <el-form-item label="半成品名称" prop="sfproductName" v-show="true">
          <el-input v-model="formOutWarehouse.sfproductName" placeholder="请输入半成品名称" :disabled="true" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="formOutWarehouse.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="formOutWarehouse.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormOutWarehouse">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  addControls,
  addCourse,
  addDetection, courseAddSfpWarehouse,
  delCourse, delDialogue,
  getControls,
  getCourse,
  getDetection,
  getIsInStock, getWarehousinginfo,
  listCourse,
  updateCourse
} from "@/api/course/course";
import {listSfproduct} from "@/api/sfproduct/sfproduct";
import {addSfpWarehouse, outSfpWarehouse} from "@/api/warehouse/sfpWarehouse";
import {addFpWarehouse} from "@/api/warehouse/fpWarehouse";

export default {
  name: "Course",
  dicts: ['lw_flow_type','lw_status_type'],
  data() {
    return {
      sfproductNames:[],
      warehouseType:null,
      cache: {}, // 缓存对象
      isInfoVisible: false,
      dialogVisible: false,
      isVisible: false,
      infoTableData: [],
      dialogTableData: [],
      Dialogue:[],
      showFormItem: false,
      first:true,
      sfproducts:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产过程管理表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openWeld:false,
      openDetection:false,
      openWarehouse:false,
      openOutWarehouse:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sfproductId: null,
        sfproductName: null,
        schedulingTime: null,
        workshopOutputNumber: null,
        solderId: null,
        firstDetectionId: null,
        assembleAgingId: null,
        twiceDetectionId: null,
        acceptanceNumber: null,
        disqualificationNumber: null,
      },
      // 表单参数
      form: {},
      formWeld: {},
      formDetection:{},
      formWarehouse:{},
      formOutWarehouse:{},
      // 表单校验
      rules: {
        sfproductId: [
          { required: true, message: "半成品不能为空", trigger: "blur" }
        ],
        schedulingTime: [
          { required: true, message: "排产时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSfProductList();
    this.clearCache(); // 在每次重新渲染时清空缓存
    // 在第一次渲染后，清空缓存对象，以便重新获取isInStock结果
    this.$nextTick(() => {
      this.clearCache();
      this.$forceUpdate(); // 强制重新渲染组件
    });
  },
  methods: {
    isInStock(row) {
      const id = row.id;
      if (this.cache[id] !== undefined) {
        // 如果缓存中存在结果，则直接返回缓存结果
        return this.cache[id];
      } else {
        // 如果缓存中不存在结果，则进行异步操作获取结果
        this.getIsInStock(id);
        return false;
      }
    },
    async getIsInStock(id) {
      try {
        const isInStock = await getIsInStock(id);
        this.cache[id] = !!isInStock; // 将结果存储到缓存对象中
        this.$forceUpdate(); // 强制重新渲染组件
      } catch (error) {
        console.error(error);
      }
    },
    clearCache() {
      this.cache = {}; // 清空缓存对象
    },
    showDetection1(row){
      this.title = "半成品检测";
      this.isVisible = true;
      const status = 0;
      getDetection(row.id,status).then(response => {
        this.Dialogue = response.data;
      });
    },
    showDetection2(row){
      this.title = "成品检测";
      this.isVisible = true;
      const status = 1;
      getDetection(row.id,status).then(response => {
        this.Dialogue = response.data;
      });
    },
    showWarehousinginfo1(row){
      this.title = "半成品入库记录";
      this.isInfoVisible = true;
      const flow = "0";
      getWarehousinginfo(row.id,flow).then(response => {
        this.infoTableData = response.data;
      });
    },
    showWarehousinginfo2(row){
      this.title = "成品入库记录";
      this.isInfoVisible = true;
      const flow = "1";
      getWarehousinginfo(row.id,flow).then(response => {
        this.infoTableData = response.data;
      });
    },
    showSolder(row){
      this.title = "后焊";
      this.dialogVisible = true;
      const flow = 0;
      getControls(row.id,flow).then(response => {
        this.dialogTableData = response.data;
      });
    },
    showAssembleAging(row){
      this.title = "装配\\老化";
      this.dialogVisible = true;
      const flow = 1;
      getControls(row.id,flow).then(response => {
        this.dialogTableData = response.data;
      });
    },
    /** 查询半成品列表 */
    getSfProductList() {
      this.loading = true;
      this.queryParams.pageSize = 1000;//查询列表大小
      listSfproduct(this.queryParams).then(response => {
        this.sfproducts = response.rows;
        this.sfproductNames = response.rows.map(product => product.sfproductName);
        this.loading = false;
      });
    },
    /** 查询生产过程管理列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        console.log(response)
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openWeld = false;
      this.openDetection = false;
      this.openWarehouse = false;
      this.openOutWarehouse = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sfproductId: null,
        sfproductName:null,
        schedulingTime: null,
        workshopOutputNumber: null,
        solderId: null,
        firstDetectionId: null,
        assembleAgingId: null,
        twiceDetectionId: null,
        acceptanceNumber: null,
        disqualificationNumber: null,
        remark: null
      };
      this.resetForm("form");
      this.formWeld = {
        courseId:null,
        time:null,
        employeeId:null,
        takeoutMaterials:null,
        flow:null,
        completed:null
      };
      this.resetForm("formWeld");
      this.formDetection = {
        courseId:null,
        time:null,
        userId:null,
        userName:null,
        acceptanceNumber: null,
        disqualificationNumber: null,
        status: null,
        remark: null
      };
      this.resetForm("formDetection");
      this.formWarehouse = {
        sfproductId:null,
        sfproductName:null,
        model:null,
        quantity:null,
        remark:null,
        ifSelfProduced:null,
        courseId:null
      };
      this.resetForm("formWarehouse");
      this.formOutWarehouse = {
        sfproductId:null,
        sfproductName:null,
        model:null,
        quantity:null,
        remark:null,
        ifSelfProduced:null,
        courseId:null
      };
      this.resetForm("formOutWarehouse");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.first = false;
      this.title = "添加生产过程管理";
    },
    /** 操作按钮操作 */
    weldAdd(row) {
      this.reset();
      this.openWeld = true;
      this.formWeld.courseId = row.id;
      this.title = "操作记录信息";
    },
    /** 检测按钮操作 */
    detectionAdd(row) {
      this.reset();
      this.openDetection = true;
      this.formDetection.courseId = row.id;
      this.title = "检测记录信息";
    },
    /** 半成品仓库入库按钮 */
    addWarehouse(row) {
      this.reset();
      this.formWarehouse.courseId = row.id;
      this.openWarehouse = true;
      this.warehouseType = 0;
      this.formWarehouse.sfproductId = row.sfproductId;
      this.formWarehouse.sfproductName = row.sfproductName;
      this.formWarehouse.model = row.model;
      this.formWarehouse.ifSelfProduced = 0;
      this.title = "半成品入库";
    },
    /** 半成品仓库出库按钮 */
    outWarehouse(row) {
      this.reset();
      this.formOutWarehouse.courseId = row.id;
      this.openOutWarehouse = true;
      this.formOutWarehouse.sfproductId = row.sfproductId;
      this.formOutWarehouse.sfproductName = row.sfproductName;
      this.formOutWarehouse.model = row.model;
      this.formOutWarehouse.ifSelfProduced = 0;
      this.title = "半成品出库";
    },
    /** 成品仓库入库按钮 */
    addFpWarehouse(row) {
      this.reset();
      this.formWarehouse.courseId = row.id;
      this.openWarehouse = true;
      this.warehouseType = 1;
      this.formWarehouse.sfproductId = row.sfproductId;
      this.formWarehouse.sfproductName = row.sfproductName;
      this.formWarehouse.model = row.model;
      this.formWarehouse.ifSelfProduced = 0;
      this.title = "成品入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.first = true;
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产过程管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitFormDetection(){
      this.$refs["formDetection"].validate(valid => {
        if (valid){
          if (this.formDetection.status === 0){
            addDetection(this.formDetection).then(response => {
              this.$modal.msgSuccess("记录成功");
              this.openDetection = false;
              this.getList();
            })
          }else if (this.formDetection.status === 1){
            addDetection(this.formDetection).then(response => {
              this.$modal.msgSuccess("记录成功");
              this.openDetection = false;
              this.getList();
            })
          }

        }
      });
    },
    submitFormWeld(){
      this.$refs["formWeld"].validate(valid => {
        if (valid) {
          if (this.formWeld.flow === '0'){
            addControls(this.formWeld).then(response => {
              console.log(response);
              this.$modal.msgSuccess("后焊记录成功");
              this.openWeld = false;
              this.getList();
            });
          }else if (this.formWeld.flow === '1'){
            addControls(this.formWeld).then(response => {
              console.log(response);
              this.$modal.msgSuccess("装配\\老化记录成功");
              this.openWeld = false;
              this.getList();
            });
          }
        }

      });
    },
    submitFormWarehouse(){
      this.$refs["formWarehouse"].validate(valid => {
        if (valid){
          if(this.warehouseType === 0){
            addSfpWarehouse(this.formWarehouse).then(response => {
              this.$modal.msgSuccess("半成品入库成功");
              this.openWarehouse = false;
              this.getList();
            });
          }else if(this.warehouseType === 1){
            addFpWarehouse(this.formWarehouse).then(response => {
              this.$modal.msgSuccess("成品入库成功");
              this.openWarehouse = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 半成品出库提交按钮 */
    submitFormOutWarehouse(){
      this.$refs["formOutWarehouse"].validate(valid => {
        if (valid){
          outSfpWarehouse(this.formOutWarehouse).then(response => {
            this.$modal.msgSuccess("出库成功");
            this.openOutWarehouse = false;
            this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除生产过程管理编号为"' + ids + '"的数据项？').then(function() {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    deleteRow(id){
      this.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function() {
        return delDialogue(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  },
};
</script>
